package com.se2.antiqueexhibition.Service.impl;

import com.se2.antiqueexhibition.Mapper.AdminCulturalRelicMapper;
import com.se2.antiqueexhibition.Service.AdminCulturalRelicService;
import com.se2.antiqueexhibition.pojo.CulturalRelic;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdminCulturalRelicServiceimpl implements AdminCulturalRelicService {

    @Resource
    private AdminCulturalRelicMapper adminCulturalRelicMapper;

    @Override
    public List<CulturalRelic> getAllRelics() {
        return adminCulturalRelicMapper.selectAll();
    }

    @Override
    public CulturalRelic getRelicById(int id) {
        return adminCulturalRelicMapper.selectById(id);
    }

    @Override
    @Transactional
    public void addRelic(CulturalRelic culturalRelic) {
        // 如果创建时间为空，设置为当前时间
        if (culturalRelic.getCreateTime() == null) {
            culturalRelic.setCreateTime(LocalDateTime.now());
        }
        // 如果状态为空，设置默认状态
        if (culturalRelic.getStatus() == null || culturalRelic.getStatus().trim().isEmpty()) {
            culturalRelic.setStatus("in_storage");
        }
        adminCulturalRelicMapper.insert(culturalRelic);
    }

    @Override
    @Transactional
    public void updateRelic(CulturalRelic culturalRelic) {
        adminCulturalRelicMapper.update(culturalRelic);
    }

    @Override
    @Transactional
    public void deleteRelic(int id) {
        adminCulturalRelicMapper.delete(id);
    }

    @Override
    public boolean hasRelicsInCategory(int categoryId) {
        return adminCulturalRelicMapper.countByCategoryId(categoryId) > 0;
    }
}

